package core.service.crud;

import java.io.Serializable;

import core.base.exception.AppBizException;
import core.dao.CrudDao;

public class SimpleCrudServiceImpl<T, I extends Serializable> implements CrudService<T, I> {
	
	private CrudDao<T, I> dao;

	protected void beforeAdd(T o) throws AppBizException {
	}

	protected void beforeUpdate(T o) throws AppBizException {
	}

	protected void afterAdd(T o) throws AppBizException {
	}

	protected void afterUpdate(T o) throws AppBizException {
	}

	protected void beforeDelete(I k) throws AppBizException {
	}

	protected void afterDelete(I k) throws AppBizException {
	}

	protected void check(T o) throws AppBizException {
	}

	public void add(T o) throws AppBizException {
		beforeAdd(o);
		dao.add(o);
		afterAdd(o);
	}

	public void delete(I k) throws AppBizException {
		beforeDelete(k);
		dao.delete(k);
		afterDelete(k);
	}

	public T get(I k) {
		return dao.get(k);
	}

	public T getAndCheck(I k) throws AppBizException {
		T o = dao.get(k);
		check(o);
		return o;
	}

	public void update(T o) throws AppBizException {
		beforeUpdate(o);
		dao.update(o);
		afterUpdate(o);
	}

	public CrudDao<T, I> getDao() {
		return dao;
	}

	public void setDao(CrudDao<T, I> dao) {
		this.dao = dao;
	}

}
